package com.selfdiscipline.data.repository

import com.selfdiscipline.data.local.dao.TimeRecordDao
import com.selfdiscipline.data.local.entity.TimeRecordEntity
import com.selfdiscipline.domain.repository.TimeRecordRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.first
import java.time.LocalDateTime
import javax.inject.Inject

class TimeRecordRepositoryImpl @Inject constructor(
    private val timeRecordDao: TimeRecordDao
) : TimeRecordRepository {
    
    override fun getAllTimeRecords(): Flow<List<TimeRecordEntity>> {
        return timeRecordDao.getAllTimeRecords()
    }
    
    override fun getCompletedTimeRecords(): Flow<List<TimeRecordEntity>> {
        return timeRecordDao.getAllTimeRecords()
    }
    
    override fun getActiveTimeRecords(): Flow<List<TimeRecordEntity>> {
        return kotlinx.coroutines.flow.flowOf(emptyList())
    }
    
    override fun getTimeRecordsInRange(startDate: LocalDateTime, endDate: LocalDateTime): Flow<List<TimeRecordEntity>> {
        return timeRecordDao.getTimeRecordsInRange(startDate, endDate)
    }
    
    override suspend fun getTimeRecordById(recordId: Long): TimeRecordEntity? {
        return timeRecordDao.getTimeRecordById(recordId).first()
    }
    
    override suspend fun insertTimeRecord(timeRecord: TimeRecordEntity): Long {
        return timeRecordDao.insert(timeRecord)
    }
    
    override suspend fun updateTimeRecord(timeRecord: TimeRecordEntity) {
        timeRecordDao.update(timeRecord)
    }
    
    override suspend fun deleteTimeRecord(timeRecord: TimeRecordEntity) {
        timeRecordDao.delete(timeRecord)
    }
    
    override suspend fun deleteTimeRecordById(recordId: Long) {
        timeRecordDao.deleteById(recordId)
    }
    
    override suspend fun getTotalDurationInRange(startDate: LocalDateTime, endDate: LocalDateTime): Long? {
        val totalMinutes = timeRecordDao.getTotalDurationMinutesInRange(startDate, endDate).first()
        return totalMinutes?.toLong()
    }
} 